﻿package controller
{
import data.RadioEvents;

import flash.errors.IOError;
	import flash.events.Event
	import flash.events.EventDispatcher
	import flash.events.IOErrorEvent
	import flash.events.ProgressEvent
	import flash.events.TimerEvent;
	import flash.media.Sound
	import flash.media.SoundChannel
	import flash.media.SoundTransform
	import flash.net.*;
	import flash.utils.*;
	
	import flash.media.SoundLoaderContext;
	import flash.system.Security;
	
	public class PlayStreamService extends EventDispatcher
	{
		private static const CLEAR_BUFFER_TIME:int = 60 * 30 * 1000;
		private var _clearBuferTimer:Timer = null;
		private var _sound:Sound = null;
		private var _soundChanel:SoundChannel;
		public function PlayStreamService() 
		{
			
		}
		
		public function play(stationURL:String):void
		{
			try {
				if (_sound) 
					_sound.close();
			}
			catch (err:Error)
			{
				//trace("we have a problem(");
			}
			var context:SoundLoaderContext = new SoundLoaderContext();
			context.checkPolicyFile = true;
			context.bufferTime = 6000;
			
			_sound = new Sound(new URLRequest(stationURL),context);
			_sound.addEventListener(ProgressEvent.PROGRESS, _bufferEvent);
			_sound.addEventListener(IOErrorEvent.IO_ERROR, _onError);
			_sound.addEventListener(IOErrorEvent.NETWORK_ERROR, _onError);
			_soundChanel = new SoundChannel();
			_soundChanel = _sound.play();
			dispatchEvent(new Event(RadioEvents.STREAMING_PLAY));
			
			
			
			if (_clearBuferTimer) {
				_clearBuferTimer.stop();
				_clearBuferTimer.removeEventListener(TimerEvent.TIMER, _onClearBuferTimerEvent);
				_clearBuferTimer = null;
			}
			_clearBuferTimer = new Timer(CLEAR_BUFFER_TIME)
			_clearBuferTimer.addEventListener(TimerEvent.TIMER, _onClearBuferTimerEvent);
			_clearBuferTimer.start();
			
		}

		private function _bufferEvent(event:ProgressEvent):void
		{
			if (!_soundChanel) return;
			var miliseconds:int = _soundChanel.position;//_sound.length;
			if (miliseconds > 0 )
			{
				if (miliseconds>  1 && miliseconds< 1000)
					dispatchEvent(new Event(RadioEvents.BUFFER_LOW));
				else if (miliseconds >  1000 && miliseconds< 2500)
					dispatchEvent(new Event(RadioEvents.BUFFER_MEDIUM));
				else if (miliseconds >  2500)
					dispatchEvent(new Event(RadioEvents.BUFFER_HIGHT));
			}
		}
		
		private function _onError(event:IOErrorEvent):void
		{
			trace("errr");
			dispatchEvent(new Event(RadioEvents.STREAM_ERROR));
		}
		
		public function stop():void {
			_soundChanel.stop();
			if (_sound) 
				_sound.close();
			_sound.removeEventListener(ProgressEvent.PROGRESS, _bufferEvent);
			_sound.removeEventListener(IOErrorEvent.IO_ERROR, _onError);
			_sound = null;
			_soundChanel = null;
		}
		
		public function set volume(value:Number):void 
		{
			if(!_sound)
				return;
			if (!_soundChanel)
				return;
			_soundChanel.soundTransform=new SoundTransform(value);
		}
		
		private function _onClearBuferTimerEvent(event:TimerEvent):void {
			dispatchEvent(new Event(RadioEvents.STREAM_REBOOT));
		}
	}
}